package com.house.framework.utils;

import com.house.framework.entity.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TreeNodeUtils
 * @description: 生成树形结构数据
 * @datetime 2022年 12月 14日 10:55
 */
public class TreeNodeUtils {

    public static List<Tree> genTree(List<Tree> treeList){
        List<Tree> trees = new ArrayList<>();
        for (Tree tree : treeList){
            if ("null" == tree.getParentId()){
                trees.add(findChildren(tree,treeList));
            }
        }
        return  trees;
    }

    public static  Tree findChildren(Tree tree,List<Tree> treeList){
        for (Tree node : treeList){
            if (tree.getId().equals(node.getParentId())){
                if (tree.getChildren() == null){
                    tree.setChildren(new ArrayList<>());
                }
                tree.getChildren().add(findChildren(node,treeList));
            }
        }
        return tree;
    }
}
